<?php
/**
 * Created by PhpStorm.
 * User: mf
 * Date: 2017/9/12
 * Time: 16:21
 */
namespace app\admin\logic;

use think\Db;
use tool\Common;

class Order extends Base
{
    //生成预订单
    public function createPreOrder()
    {
        $roleID = $this->getRoleID();
        if ($roleID != 4 && $roleID != 5) {
            return Common::rm(-2,'对不起，您没有权限操作');
        }
        $check = $this->check(__FUNCTION__);
        if ($check['code'] != 1) {
            return $check;
        }
        $teamID = $this->getUserID();
        if ($this->data['cartID'] && $this->data['sellerID'] && $this->data['productID'] && $this->data['number'] && $this->data['price'] && $this->data['totalPrice']) {
            $cartIDInfo = Db::name('shopping_cart')->where('id',$this->data['cartID'])->find();
            if ($cartIDInfo['teamID'] != $teamID) {
                return Common::rm(-2,'您没有权限添加');
            }
            if ($cartIDInfo['status'] != 0) {
                return Common::rm('-2','添加失败，已添加到预订单');
            }
            $ar = [
                'cartID' => $this->data['cartID'],
                'teamID' => $teamID,
                'sellerID' => $this->data['sellerID'],
                'productID' => $this->data['productID'],
                'number' => $this->data['number'],
                'price' => $this->data['price'],
                'totalPrice' => $this->data['totalPrice'],
                'addtime' => THINK_START_TIME
            ];
            Db::name('shopping_cart')->where('id',$this->data['cartID'])->update(['status' => 1]);
            $res = Db::name('pre_order')->insert($ar);
            if ($res) {
                return Common::rm(1,'生成预订单成功');
            } else {
                return Common::rm(-2,'生成预订单失败');
            }
        } else {
            return Common::rm(-2,'生成预订单失败');
        }
    }

    //得到预订单列表
    public function getPreorderList()
    {
        $roleID = $this->getRoleID();
        if ($roleID != 2 && $roleID != 4 && $roleID != 5) {
            return Common::rm(-2,'您没有权限操作');
        }
        $userID = $this->getUserID();
        $sql = Db::view('pre_order',['id' => 'preOrderID','number,price,totalPrice,addtime'])
            ->view('user','sellerName','user.id = pre_order.sellerID')
            ->view('product',['name' => 'productName'],'product.id = pre_order.productID')
            ->order('pre_order.addtime desc');
        if ($roleID == 2) {
            $preOrderList = $sql->where('pre_order.sellerID',$userID)->select();
        } else {
            $preOrderList = $sql->where('teamID',$userID)->select();
        }
        foreach ($preOrderList as $key => $val) {
            $preOrderList[$key]['addtime'] = date('Y-m-d H:i:s',$val['addtime']);
        }

    }

    //修改预订单
    public function updatePreOrder()
    {
        $roleID = $this->getRoleID();

    }
}